package com.matchmealpha;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TimelineView extends LinearLayout implements OnClickListener {

	private TextView timeText;
	private TextView user1;
	private TextView user2;
	private TextView location1;
	private TextView location2;
	private ImageView image;
	private int callerID;
	private Context context;
	private Post post;
	private int position;

	public TimelineView(Context context, Post post, int callerID, int position) {
		super(context);
		/* inflating layout */
		this.context = context;
		this.callerID = callerID;
		this.post = post;
		this.position = position;
		initLayout();
		setTimelineItem(post,position);
		initListener();
	}

	private void initLayout() {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.timeline_item_view, this, true);
		this.timeText = (TextView) this.findViewById(R.id.TimelineItemTimeText);
		this.user1 = (TextView) this.findViewById(R.id.TimelineItemUser1);
		this.user2 = (TextView) this.findViewById(R.id.TimelineItemUser2);
		this.location1 = (TextView) this
				.findViewById(R.id.TimelineItemLocation1);
		this.location2 = (TextView) this
				.findViewById(R.id.TimelineItemLocation2);
		this.image = (ImageView) this.findViewById(R.id.TimelineItemImage);
		
	}

	private void initListener() {
		this.user1.setOnClickListener(this);
		this.user2.setOnClickListener(this);
		this.location1.setOnClickListener(this);
		this.location2.setOnClickListener(this);
		this.image.setOnClickListener(this);
	}

	public void setTimelineItem(Post post, int position2) {
		Log.d("TIMELINE VIEW-------", "POSITION 2 :" + position2);
		this.position = position2;
		this.user1.setText(post.getUserID1());
		this.user2.setText(post.getUserID2());
		this.location1.setText(post.getLocation1());
		this.location2.setText(post.getLocation2());
		this.timeText.setText(post.getTime1());
		this.image.setImageBitmap(post.getBitMapImage());
	// bm.recycle();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case (R.id.TimelineItemUser1):
			Intent intent5 = new Intent(this.context, TimelineActivity.class);
			intent5.putExtra("back", true);
			intent5.putExtra("callerID", this.callerID);
			View view5 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity(
							"TimelineActivity",
							intent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view5, this.callerID);
			break;
		case (R.id.TimelineItemUser2):
			Intent intent2 = new Intent(this.context, TimelineActivity.class);
			intent2.putExtra("back", true);
			intent2.putExtra("callerID", this.callerID);
			View view2 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity(
							"TimelineActivity",
							intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view2, this.callerID);
			break;
		/*case (R.id.TimelineItemLocation1):
			Intent intent1 = new Intent(this.context, PostActivity.class);
			intent1.putExtra("callerID", this.callerID);
			View view1 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity("PostActivity",
							intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view1, this.callerID);
			break;*/
		case (R.id.TimelineItemImage):
			Intent intent6 = new Intent(this.context, PostActivity.class);
			intent6.putExtra("back", true);
			intent6.putExtra("callerID", this.callerID);
			intent6.putExtra("postIndex", this.position);
			View view6 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity("PostActivity",
							intent6.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view6, this.callerID);
			break;
		}
	}

}
